package com.example.demo.controller;
import com.example.demo.entity.UserInfo;
import com.example.demo.enums.UserEnum;
import com.example.demo.form.UserInfoForm;
import com.example.demo.service.PersonResultService;
import com.example.demo.service.UserInfoService;
import com.example.demo.utils.CheckUtil;
import com.example.demo.utils.HttpRequestUtil;
import com.example.demo.utils.WechatAPI;
import com.example.demo.vo.RecordVO;
import com.example.demo.vo.UserResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Date;

@RestController
@Slf4j
@Api(tags = "微信小程序登陆")
public class WeiXinController {
    @Autowired
    private UserInfoService userInfoService;
    @GetMapping("/getAuthorization")
    @ApiIgnore
    public void getAuthorization(HttpServletRequest request, HttpServletResponse resp)throws Exception{
        String signature = request.getParameter("signature");
        String timestamp = request.getParameter("timestamp");
        String nonce = request.getParameter("nonce");
        String echostr = request.getParameter("echostr");
        log.info("wechat info,signature={},nonce={},timestamp={}",signature,nonce,timestamp);
        PrintWriter out = resp.getWriter();
        if(CheckUtil.checkSignature(signature,timestamp,nonce)){
            out.print(echostr);
        }
    }
    @GetMapping("/getWechatUserInfo")
    @ApiOperation("开放接口->wx.login()")
    public String getWechatUserInfo(HttpServletRequest request, HttpServletResponse resp)throws Exception{
        String code = request.getParameter("code");
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("https://api.weixin.qq.com/sns/jscode2session?appid=");
        stringBuffer.append(WechatAPI.APP_ID);
        stringBuffer.append("&secret=");
        stringBuffer.append(WechatAPI.APP_SECRET);
        stringBuffer.append("&js_code=");
        stringBuffer.append(code);
        stringBuffer.append("&grant_type=authorization_code");
        String userInfo = HttpRequestUtil.getXpath(stringBuffer.toString());
        log.info("userinfo={}",userInfo);
        System.out.println(userInfo);
        return userInfo;
    }
    @PostMapping("/saveUserInfo")
    @ApiOperation("保存用户信息(重复保存直接报错不任何处理)")
    public UserResultVO saveUserInfo(@Valid UserInfoForm userInfoForm, BindingResult bindingResult){
        UserResultVO userResultVO = new UserResultVO();
        if(bindingResult.hasErrors()) {
            log.error("userInfo save error ");
            userResultVO.setCode(UserEnum.error.getCode());
            userResultVO.setMsg(UserEnum.error.getMessage());
            userResultVO.setUserInfo(null);
            return  userResultVO;
        }else{
            UserInfo userInfo = new UserInfo();
            BeanUtils.copyProperties(userInfoForm,userInfo);
            userInfo.setCreateTime(new Date());
            log.info("userinfo={}",userInfo);
            userInfoService.save(userInfo);
            userResultVO.setCode(UserEnum.right.getCode());
            userResultVO.setMsg(UserEnum.right.getMessage());
            userResultVO.setUserInfo(userInfo);
            return  userResultVO;
        }
    }

    @GetMapping("/getPhoto")
    @ApiIgnore
    public void getPhoto(@RequestParam("photoId") String phototId,HttpServletResponse response) throws  Exception{
        //读取本地图片输入流
        FileInputStream inputStream = new FileInputStream("C:/testFile/"+phototId);
        int i = inputStream.available();
        //byte数组用于存放图片字节数据
        byte[] buff = new byte[i];
        inputStream.read(buff);
        //记得关闭输入流
        inputStream.close();
        //设置发送到客户端的响应内容类型
        response.setContentType("image/*");
        OutputStream out = response.getOutputStream();
        out.write(buff);
        //关闭响应输出流
        out.close();
    }

}
